/*------------------------------------------------------------------------*/
/*  Copyright 2014 National Renewable Energy Laboratory.                  */
/*  This software is released under the license detailed                  */
/*  in the file, LICENSE, which is located in the top-level Nalu          */
/*  directory structure                                                   */
/*------------------------------------------------------------------------*/

#include "kernels/UnitTestKernelUtils.h"
#include "UnitTestUtils.h"
#include "UnitTestHelperObjects.h"

#include "kernel/MomentumAdvDiffElemKernel.h"

namespace {
namespace hex8_golds {
namespace advection_diffusion {
static constexpr double rhs[24] = {
  0.0102032025649709,  0.0060345933621288,  0.0000000000000000,
  0.0380143337648886,  -0.0871643624304565, 0.0000000000000000,
  0.0097665616807021,  0.0389468261005970,  0.0000000000000000,
  -0.0579840980105616, 0.0421829429677307,  0.0000000000000000,
  0.0102032025649709,  0.0060345933621288,  0.0000000000000000,
  0.0380143337648886,  -0.0871643624304565, 0.0000000000000000,
  0.0097665616807021,  0.0389468261005970,  0.0000000000000000,
  -0.0579840980105616, 0.0421829429677307,  0.0000000000000000,
};

static constexpr double lhs[24][24] = {
  {
    0.0562500000000000,  0.0093750000000000,  0.0093750000000000,
    -0.0262766323389992, -0.0093750000000000, -0.0093750000000000,
    -0.0125000000000000, -0.0093750000000000, -0.0031250000000000,
    0.0075266323389992,  0.0093750000000000,  0.0031250000000000,
    0.0000000000000000,  0.0031250000000000,  0.0093750000000000,
    -0.0150088774463331, -0.0031250000000000, -0.0093750000000000,
    -0.0062500000000000, -0.0031250000000000, -0.0031250000000000,
    -0.0037411225536669, 0.0031250000000000,  0.0031250000000000,
  },
  {
    0.0093750000000000,  0.0562500000000000,  0.0093750000000000,
    0.0093750000000000,  -0.0075266323389992, 0.0031250000000000,
    -0.0093750000000000, -0.0125000000000000, -0.0031250000000000,
    -0.0093750000000000, -0.0112233676610008, -0.0093750000000000,
    0.0031250000000000,  0.0000000000000000,  0.0093750000000000,
    0.0031250000000000,  -0.0087588774463331, 0.0031250000000000,
    -0.0031250000000000, -0.0062500000000000, -0.0031250000000000,
    -0.0031250000000000, -0.0099911225536669, -0.0093750000000000,
  },
  {
    0.0093750000000000,  0.0093750000000000,  0.0562500000000000,
    0.0093750000000000,  0.0031250000000000,  -0.0075266323389992,
    0.0031250000000000,  0.0031250000000000,  -0.0062500000000000,
    0.0031250000000000,  0.0093750000000000,  0.0075266323389992,
    -0.0093750000000000, -0.0093750000000000, -0.0187500000000000,
    -0.0093750000000000, -0.0031250000000000, -0.0150088774463331,
    -0.0031250000000000, -0.0031250000000000, -0.0062500000000000,
    -0.0031250000000000, -0.0093750000000000, -0.0099911225536669,
  },
  {
    0.0038298970169975,  0.0093750000000000,  0.0093750000000000,
    0.1014097940339950,  -0.0093750000000000, -0.0093750000000000,
    0.0376331616949958,  -0.0093750000000000, -0.0031250000000000,
    0.0025532646779983,  0.0093750000000000,  0.0031250000000000,
    -0.0049733676610008, 0.0031250000000000,  0.0093750000000000,
    0.0150532646779983,  -0.0031250000000000, -0.0093750000000000,
    0.0062943872316653,  -0.0031250000000000, -0.0031250000000000,
    -0.0012322451073339, 0.0031250000000000,  0.0031250000000000,
  },
  {
    -0.0093750000000000, 0.0225798970169975,  0.0031250000000000,
    -0.0093750000000000, 0.1014097940339950,  0.0093750000000000,
    0.0093750000000000,  0.0188831616949958,  -0.0093750000000000,
    0.0093750000000000,  0.0025532646779983,  -0.0031250000000000,
    -0.0031250000000000, 0.0012766323389992,  0.0031250000000000,
    -0.0031250000000000, 0.0150532646779983,  0.0093750000000000,
    0.0031250000000000,  0.0000443872316653,  -0.0093750000000000,
    0.0031250000000000,  -0.0012322451073339, -0.0031250000000000,
  },
  {
    -0.0093750000000000, 0.0031250000000000,  0.0225798970169975,
    -0.0093750000000000, 0.0093750000000000,  0.1014097940339950,
    -0.0031250000000000, 0.0093750000000000,  0.0376331616949958,
    -0.0031250000000000, 0.0031250000000000,  0.0088032646779983,
    0.0093750000000000,  -0.0031250000000000, -0.0049733676610008,
    0.0093750000000000,  -0.0093750000000000, -0.0036967353220017,
    0.0031250000000000,  -0.0093750000000000, 0.0000443872316653,
    0.0031250000000000,  -0.0031250000000000, -0.0012322451073339,
  },
  {
    -0.0125000000000000, -0.0093750000000000, 0.0031250000000000,
    -0.0225798970169975, 0.0093750000000000,  -0.0031250000000000,
    0.0562500000000000,  0.0093750000000000,  -0.0093750000000000,
    0.0038298970169975,  -0.0093750000000000, 0.0093750000000000,
    -0.0062500000000000, -0.0031250000000000, 0.0031250000000000,
    -0.0137766323389992, 0.0031250000000000,  -0.0031250000000000,
    0.0000000000000000,  0.0031250000000000,  -0.0093750000000000,
    -0.0049733676610008, -0.0031250000000000, 0.0093750000000000,
  },
  {
    -0.0093750000000000, -0.0125000000000000, 0.0031250000000000,
    -0.0093750000000000, -0.0413298970169975, 0.0093750000000000,
    0.0093750000000000,  0.0562500000000000,  -0.0093750000000000,
    0.0093750000000000,  0.0225798970169975,  -0.0031250000000000,
    -0.0031250000000000, -0.0062500000000000, 0.0031250000000000,
    -0.0031250000000000, -0.0200266323389992, 0.0093750000000000,
    0.0031250000000000,  0.0000000000000000,  -0.0093750000000000,
    0.0031250000000000,  0.0012766323389992,  -0.0031250000000000,
  },
  {
    -0.0031250000000000, -0.0031250000000000, -0.0062500000000000,
    -0.0031250000000000, -0.0093750000000000, -0.0225798970169975,
    -0.0093750000000000, -0.0093750000000000, 0.0562500000000000,
    -0.0093750000000000, -0.0031250000000000, 0.0225798970169975,
    0.0031250000000000,  0.0031250000000000,  -0.0062500000000000,
    0.0031250000000000,  0.0093750000000000,  -0.0200266323389992,
    0.0093750000000000,  0.0093750000000000,  -0.0187500000000000,
    0.0093750000000000,  0.0031250000000000,  -0.0049733676610008,
  },
  {
    -0.0225798970169975, -0.0093750000000000, 0.0031250000000000,
    -0.0275532646779983, 0.0093750000000000,  -0.0031250000000000,
    -0.0563831616949958, 0.0093750000000000,  -0.0093750000000000,
    0.0110902059660050,  -0.0093750000000000, 0.0093750000000000,
    -0.0137766323389992, -0.0031250000000000, 0.0031250000000000,
    -0.0112677548926661, 0.0031250000000000,  -0.0031250000000000,
    -0.0250443872316653, 0.0031250000000000,  -0.0093750000000000,
    -0.0150532646779983, -0.0031250000000000, 0.0093750000000000,
  },
  {
    0.0093750000000000,  -0.0413298970169975, 0.0093750000000000,
    0.0093750000000000,  -0.0275532646779983, 0.0031250000000000,
    -0.0093750000000000, -0.0376331616949958, -0.0031250000000000,
    -0.0093750000000000, 0.0110902059660050,  -0.0093750000000000,
    0.0031250000000000,  -0.0200266323389992, 0.0093750000000000,
    0.0031250000000000,  -0.0112677548926661, 0.0031250000000000,
    -0.0031250000000000, -0.0187943872316653, -0.0031250000000000,
    -0.0031250000000000, -0.0150532646779983, -0.0093750000000000,
  },
  {
    0.0031250000000000,  -0.0093750000000000, -0.0225798970169975,
    0.0031250000000000,  -0.0031250000000000, -0.0213032646779983,
    0.0093750000000000,  -0.0031250000000000, -0.0376331616949958,
    0.0093750000000000,  -0.0093750000000000, 0.0110902059660050,
    -0.0031250000000000, 0.0093750000000000,  -0.0200266323389992,
    -0.0031250000000000, 0.0031250000000000,  -0.0112677548926661,
    -0.0093750000000000, 0.0031250000000000,  -0.0250443872316653,
    -0.0093750000000000, 0.0093750000000000,  -0.0338032646779983,
  },
  {
    0.0000000000000000,  0.0031250000000000,  -0.0093750000000000,
    -0.0150088774463331, -0.0031250000000000, 0.0093750000000000,
    -0.0062500000000000, -0.0031250000000000, 0.0031250000000000,
    -0.0037411225536669, 0.0031250000000000,  -0.0031250000000000,
    0.0562500000000000,  0.0093750000000000,  -0.0093750000000000,
    -0.0262766323389992, -0.0093750000000000, 0.0093750000000000,
    -0.0125000000000000, -0.0093750000000000, 0.0031250000000000,
    0.0075266323389992,  0.0093750000000000,  -0.0031250000000000,
  },
  {
    0.0031250000000000,  0.0000000000000000,  -0.0093750000000000,
    0.0031250000000000,  -0.0087588774463331, -0.0031250000000000,
    -0.0031250000000000, -0.0062500000000000, 0.0031250000000000,
    -0.0031250000000000, -0.0099911225536669, 0.0093750000000000,
    0.0093750000000000,  0.0562500000000000,  -0.0093750000000000,
    0.0093750000000000,  -0.0075266323389992, -0.0031250000000000,
    -0.0093750000000000, -0.0125000000000000, 0.0031250000000000,
    -0.0093750000000000, -0.0112233676610008, 0.0093750000000000,
  },
  {
    0.0093750000000000,  0.0093750000000000,  -0.0187500000000000,
    0.0093750000000000,  0.0031250000000000,  -0.0150088774463331,
    0.0031250000000000,  0.0031250000000000,  -0.0062500000000000,
    0.0031250000000000,  0.0093750000000000,  -0.0099911225536669,
    -0.0093750000000000, -0.0093750000000000, 0.0562500000000000,
    -0.0093750000000000, -0.0031250000000000, -0.0075266323389992,
    -0.0031250000000000, -0.0031250000000000, -0.0062500000000000,
    -0.0031250000000000, -0.0093750000000000, 0.0075266323389992,
  },
  {
    -0.0049733676610008, 0.0031250000000000,  -0.0093750000000000,
    0.0150532646779983,  -0.0031250000000000, 0.0093750000000000,
    0.0062943872316653,  -0.0031250000000000, 0.0031250000000000,
    -0.0012322451073339, 0.0031250000000000,  -0.0031250000000000,
    0.0038298970169975,  0.0093750000000000,  -0.0093750000000000,
    0.1014097940339950,  -0.0093750000000000, 0.0093750000000000,
    0.0376331616949958,  -0.0093750000000000, 0.0031250000000000,
    0.0025532646779983,  0.0093750000000000,  -0.0031250000000000,
  },
  {
    -0.0031250000000000, 0.0012766323389992,  -0.0031250000000000,
    -0.0031250000000000, 0.0150532646779983,  -0.0093750000000000,
    0.0031250000000000,  0.0000443872316653,  0.0093750000000000,
    0.0031250000000000,  -0.0012322451073339, 0.0031250000000000,
    -0.0093750000000000, 0.0225798970169975,  -0.0031250000000000,
    -0.0093750000000000, 0.1014097940339950,  -0.0093750000000000,
    0.0093750000000000,  0.0188831616949958,  0.0093750000000000,
    0.0093750000000000,  0.0025532646779983,  0.0031250000000000,
  },
  {
    -0.0093750000000000, 0.0031250000000000,  -0.0049733676610008,
    -0.0093750000000000, 0.0093750000000000,  -0.0036967353220017,
    -0.0031250000000000, 0.0093750000000000,  0.0000443872316653,
    -0.0031250000000000, 0.0031250000000000,  -0.0012322451073339,
    0.0093750000000000,  -0.0031250000000000, 0.0225798970169975,
    0.0093750000000000,  -0.0093750000000000, 0.1014097940339950,
    0.0031250000000000,  -0.0093750000000000, 0.0376331616949958,
    0.0031250000000000,  -0.0031250000000000, 0.0088032646779983,
  },
  {
    -0.0062500000000000, -0.0031250000000000, -0.0031250000000000,
    -0.0137766323389992, 0.0031250000000000,  0.0031250000000000,
    0.0000000000000000,  0.0031250000000000,  0.0093750000000000,
    -0.0049733676610008, -0.0031250000000000, -0.0093750000000000,
    -0.0125000000000000, -0.0093750000000000, -0.0031250000000000,
    -0.0225798970169975, 0.0093750000000000,  0.0031250000000000,
    0.0562500000000000,  0.0093750000000000,  0.0093750000000000,
    0.0038298970169975,  -0.0093750000000000, -0.0093750000000000,
  },
  {
    -0.0031250000000000, -0.0062500000000000, -0.0031250000000000,
    -0.0031250000000000, -0.0200266323389992, -0.0093750000000000,
    0.0031250000000000,  0.0000000000000000,  0.0093750000000000,
    0.0031250000000000,  0.0012766323389992,  0.0031250000000000,
    -0.0093750000000000, -0.0125000000000000, -0.0031250000000000,
    -0.0093750000000000, -0.0413298970169975, -0.0093750000000000,
    0.0093750000000000,  0.0562500000000000,  0.0093750000000000,
    0.0093750000000000,  0.0225798970169975,  0.0031250000000000,
  },
  {
    -0.0031250000000000, -0.0031250000000000, -0.0062500000000000,
    -0.0031250000000000, -0.0093750000000000, -0.0200266323389992,
    -0.0093750000000000, -0.0093750000000000, -0.0187500000000000,
    -0.0093750000000000, -0.0031250000000000, -0.0049733676610008,
    0.0031250000000000,  0.0031250000000000,  -0.0062500000000000,
    0.0031250000000000,  0.0093750000000000,  -0.0225798970169975,
    0.0093750000000000,  0.0093750000000000,  0.0562500000000000,
    0.0093750000000000,  0.0031250000000000,  0.0225798970169975,
  },
  {
    -0.0137766323389992, -0.0031250000000000, -0.0031250000000000,
    -0.0112677548926661, 0.0031250000000000,  0.0031250000000000,
    -0.0250443872316653, 0.0031250000000000,  0.0093750000000000,
    -0.0150532646779983, -0.0031250000000000, -0.0093750000000000,
    -0.0225798970169975, -0.0093750000000000, -0.0031250000000000,
    -0.0275532646779983, 0.0093750000000000,  0.0031250000000000,
    -0.0563831616949958, 0.0093750000000000,  0.0093750000000000,
    0.0110902059660050,  -0.0093750000000000, -0.0093750000000000,
  },
  {
    0.0031250000000000,  -0.0200266323389992, -0.0093750000000000,
    0.0031250000000000,  -0.0112677548926661, -0.0031250000000000,
    -0.0031250000000000, -0.0187943872316653, 0.0031250000000000,
    -0.0031250000000000, -0.0150532646779983, 0.0093750000000000,
    0.0093750000000000,  -0.0413298970169975, -0.0093750000000000,
    0.0093750000000000,  -0.0275532646779983, -0.0031250000000000,
    -0.0093750000000000, -0.0376331616949958, 0.0031250000000000,
    -0.0093750000000000, 0.0110902059660050,  0.0093750000000000,
  },
  {
    0.0031250000000000,  -0.0093750000000000, -0.0200266323389992,
    0.0031250000000000,  -0.0031250000000000, -0.0112677548926661,
    0.0093750000000000,  -0.0031250000000000, -0.0250443872316653,
    0.0093750000000000,  -0.0093750000000000, -0.0338032646779983,
    -0.0031250000000000, 0.0093750000000000,  -0.0225798970169975,
    -0.0031250000000000, 0.0031250000000000,  -0.0213032646779983,
    -0.0093750000000000, 0.0031250000000000,  -0.0376331616949958,
    -0.0093750000000000, 0.0093750000000000,  0.0110902059660050,
  },
};
} // advection_diffusion
} // hex8_golds
} // anonymous namespace

TEST_F(MomentumKernelHex8Mesh, advection_diffusion)
{
  fill_mesh_and_init_fields();

  // Setup solution options for default advection kernel
  solnOpts_.meshMotion_ = false;
  solnOpts_.meshDeformation_ = false;
  solnOpts_.externalMeshDeformation_ = false;
  solnOpts_.includeDivU_ = 0.0;

  unit_test_utils::HelperObjects helperObjs(bulk_, stk::topology::HEX_8, 3, partVec_[0]);

  // Initialize the kernel
  std::unique_ptr<sierra::nalu::Kernel> kernel(
    new sierra::nalu::MomentumAdvDiffElemKernel<sierra::nalu::AlgTraitsHex8>(
      bulk_, solnOpts_, velocity_, viscosity_,
      helperObjs.assembleElemSolverAlg->dataNeededByKernels_));

  // Add to kernels to be tested
  helperObjs.assembleElemSolverAlg->activeKernels_.push_back(kernel.get());

  // Populate LHS and RHS
  helperObjs.assembleElemSolverAlg->execute();

  EXPECT_EQ(helperObjs.linsys->lhs_.extent(0), 24u);
  EXPECT_EQ(helperObjs.linsys->lhs_.extent(1), 24u);
  EXPECT_EQ(helperObjs.linsys->rhs_.extent(0), 24u);

  namespace gold_values = ::hex8_golds::advection_diffusion;
  unit_test_kernel_utils::expect_all_near(helperObjs.linsys->rhs_, gold_values::rhs);
  unit_test_kernel_utils::expect_all_near(helperObjs.linsys->lhs_, gold_values::lhs);
}

